package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    /**
     * 新建套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(StatusConstant.ENABLE);
        //插入套餐数据
        setmealMapper.insert(setmeal);
        //获取套餐id
        Long setmealId=setmeal.getId();
        //插入套餐菜品数据
        List<SetmealDish> dishes =setmealDTO.getSetmealDishes();
        if (dishes!=null && dishes.size()>0){
            dishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.insertBatch(dishes);
        }
    }

    /**
     * 分页查找
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult PageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.PageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //查看套餐状态--如果在售就不能删除
        for (Long id : ids) {
            Setmeal setmeal=setmealMapper.getById(id);
            if(setmeal.getStatus()==StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐表中的套餐数据
        setmealMapper.deleteBatch(ids);

        //删除套餐食物中的数据
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 启用停用套餐
     * @param status
     * @param id
     */
    @Override
    public void startOrstop(Integer status, Long id) {
        setmealMapper.startOrstop(status,id);
    }




    /**
     * 根据id查找套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        //查询套餐数据
        Setmeal setmeal=setmealMapper.getById(id);
        //查询套餐菜品数据
        List<SetmealDish> setmealDishes =setmealDishMapper.getSetmealdishBySetmealId(id);
        //封装到SetmealVo中
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //修改setmeal表中的数据
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //删除setmeal_dish表中的套餐数据
        Long setmealId=setmealDTO.getId();
        setmealDishMapper.deleteBySetmealId(setmealId);
        //插入setmeal_dish表中的套餐数据
        List<SetmealDish> dishes =setmealDTO.getSetmealDishes();
        if (dishes!=null && dishes.size()>0){
            dishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.insertBatch(dishes);
        }
    }


    /**
     * 根据套餐id查找所有菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemBySetmealId(Long id) {

        return setmealMapper.getDishItemBySetmealId(id);
    }

    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }


}
